

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.sql.SQLException;
import java.util.*;

public class Json解析 {




    public static void main(String[] args) throws SQLException {

        List keyList = new ArrayList();
        List valueList = new ArrayList();
        String jsonString = "";
        jsonString  = readFileContent("C:\\Users\\ZCL\\Desktop\\json.txt");
        String[] values = jsonString.split(",");
        for (String value :values) {
            if(value.contains("\"label\"")){
                keyList.add(value.split(":")[1]);
            }
            if(value.contains("\"id\"")){
               String temp[] = value.split(":");
                if(temp.length>2){
                    for (int i= 0 ;i<temp.length;i++) {
                        if(temp[i].contains("\"id\"")) {
                            temp[i+1].replaceAll("\"","");
                            continue;
                        }
                    }
                }else{
                    valueList.add(temp[1].replaceAll("\"",""));
                }

            }

        }
        if(keyList.size() == valueList.size()){
            String sql = "";
            for(int i=0 ;i<keyList.size();i++){
                sql+= " WHEN c.question_id = '"+keyList.get(i)+"' THEN" +
                        "c.question_answer ELSE NULL " +
                        "tEND AS '"+valueList.get(i)+"',";
            }
            System.out.println(sql);
        };





    }


    public static String readFileContent(String fileName) {
        File file = new File(fileName);
        BufferedReader reader = null;
        StringBuffer sbf = new StringBuffer();
        try {
            reader = new BufferedReader(new FileReader(file));
            String tempStr;
            while ((tempStr = reader.readLine()) != null) {
                sbf.append(tempStr);
            }
            reader.close();
            return sbf.toString();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
        return sbf.toString();
    }
}
